﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;

namespace TimeTableEasy
{
    public partial class User_Choose : Form
    {
        private int ID = 0;
        private int TypeAcc = 0;
        private String action = "";
        private ArrayList IDList = new ArrayList();

        public User_Choose()
        {
            InitializeComponent();
        }

        public User_Choose(string act)
        {
            InitializeComponent();
            action = act;
        }

        private void cmbOk_Click(object sender, EventArgs e)
        {
            bool nErr = false ;

            if (action == "mod")
                new User_Edit("mod", ID, TypeAcc).Show();

            if (action == "del")
            {
                switch (TypeAcc)
                {
                        //ADMIN
                    case 0:
                        OracleDataReader admin0 = OracleConnector.Query("DELETE FROM ADMINISTRATEUR WHERE adm_Num = " + ID);
                        if (admin0 == null)
                        {nErr = true; return;}
                        else 
                            nErr = false;

                        OracleDataReader admin1 = OracleConnector.Query("DELETE FROM TYPES WHERE Typ_ID = " + ID);
                        if (admin1 == null)
                        {nErr = true; return;}
                        else 
                            nErr = false; 

                        OracleDataReader admin2 = OracleConnector.Query("DELETE FROM IDENTITE WHERE Ide_ID = " + ID);
                        if (admin2 == null)
                        { nErr = true; return; }
                        else nErr = false;
                    break;
                        //CAMPUS MANAGER
                    case 1:
                        OracleDataReader campus0 = OracleConnector.Query("DELETE FROM CM_CAMPUS WHERE Ger_CmNum = " + ID);
                        if (campus0 == null)
                        { nErr = true; return; }
                        else nErr = false;

                        OracleDataReader campus1 = OracleConnector.Query("DELETE FROM CAMPUSMANAGER WHERE Cm_Num = " + ID);
                        if (campus1 == null)
                        { nErr = true; return; }
                        else nErr = false;

                        OracleDataReader campus2 = OracleConnector.Query("DELETE FROM TYPES WHERE Typ_ID = " + ID);
                        if (campus2 == null)
                        { nErr = true; return; }
                        else nErr = false;

                        OracleDataReader campus3 = OracleConnector.Query("DELETE FROM IDENTITE WHERE Ide_ID = " + ID);
                        if (campus3 == null)
                        { nErr = true; return; }
                        else nErr = false;                        
                    break;
                        //PROF
                    case 2:
                        OracleDataReader prof0 = OracleConnector.Query("DELETE FROM DISPENSER WHERE Dis_EnseignantNum = " + ID);
                        if (prof0 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader prof1 = OracleConnector.Query("DELETE FROM ETUDIANT_ENSEIGNANT WHERE EE_EnseignantNum = " + ID);
                        if (prof1 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader prof2 = OracleConnector.Query("DELETE FROM ENSEIGNANT WHERE Ens_Num = " + ID);
                        if (prof2 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader prof3 = OracleConnector.Query("DELETE FROM TYPES WHERE Typ_ID = " + ID);
                        if (prof3 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader prof4 = OracleConnector.Query("DELETE FROM IDENTITE WHERE Ide_ID = " + ID);
                        if (prof4 == null)
                        { nErr = true; return; }
                        else { nErr = false; }
                    break;
                        //ETUDIANT
                    case 3:

                        OracleDataReader stud0 = OracleConnector.Query("DELETE FROM ETUDIANT_CURSUS WHERE EC_EtudiantNum = " + ID);
                        if (stud0 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader stud1 = OracleConnector.Query("DELETE FROM ETUDIANT_CAMPUS_PERIODE WHERE ECP_EtudiantNum =  " + ID);
                        if (stud1 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader stud2 = OracleConnector.Query("DELETE FROM ETUDIANT_COURS WHERE ECO_EtudiantNum = " + ID);
                        if (stud2 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader stud3 = OracleConnector.Query("DELETE FROM ETUDIANT_ENSEIGNANT WHERE EE_EtudiantNum = " + ID);
                        if (stud3 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader stud4 = OracleConnector.Query("DELETE FROM ETUDIANT WHERE Etu_Num = " + ID);
                        if (stud4 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader stud5 = OracleConnector.Query("DELETE FROM TYPES WHERE Typ_ID =  " + ID);
                        if (stud5 == null)
                        { nErr = true; return; }
                        else { nErr = false; }

                        OracleDataReader stud6 = OracleConnector.Query("DELETE FROM IDENTITE WHERE Ide_ID = " + ID);
                        if (stud6 == null)
                        { nErr = true; return; }
                        else { nErr = false; }
                        break;
                }
                if (nErr == false)
                {
                    MessageBox.Show("Utilisateur " + ID + " supprimé avec succès.", "Utilisateur supprimé");
                    ClearCtrl();
                }
                else
                {
                    MessageBox.Show("Erreur lors de la suppression", "ERREUR");
                }
            }
            
        }

        private void chooseUser_Load(object sender, EventArgs e)
        {
            cmbClasses.Hide();
        }

        private void cmbTypeAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Admin Global
            ClearCtrl();
            if (cmbTypeAccount.SelectedIndex == 0)
            {
                cmbClasses.Enabled = false;
                cmbCampus.Enabled = false;
                cmbCursus.Enabled = false;
                cmbUser.Enabled = true;
                OracleDataReader array = OracleConnector.Query("select Ide_ID, Ide_Nom, Ide_Prenom from IDENTITE WHERE Ide_ID IN (select Adm_Num from ADMINISTRATEUR)");
                if (array == null) return;
                IDList.Clear();
                while (array.Read())
                {
                    cmbUser.Items.Add((int)array.GetDecimal(0) + " - " + array.GetString(1) + " " + array.GetString(2));
                    IDList.Add((int)array.GetDecimal(0));
                }
                TypeAcc = cmbTypeAccount.SelectedIndex;
                 
            }

            //Campus Manager
            if (cmbTypeAccount.SelectedIndex == 1)
            {
                cmbCampus.Enabled = true;
                cmbClasses.Enabled = false;
                cmbCursus.Enabled = false;
                cmbUser.Enabled = true;
                //CampusList
                OracleDataReader array = OracleConnector.Query("SELECT * FROM CAMPUS");

                if (array == null) return;

                while (array.Read())
                {
                    cmbCampus.Items.Add(array.GetString(1));
                }

                TypeAcc = cmbTypeAccount.SelectedIndex;

                array.Dispose();
            }

            //Intervenant
            
            if (cmbTypeAccount.SelectedIndex == 2)
            {
                cmbClasses.Enabled = false;
                cmbCampus.Enabled = false;
                cmbCursus.Enabled = false;
                cmbUser.Enabled = true;
                OracleDataReader array = OracleConnector.Query("SELECT Ide_ID, Ide_Nom, Ide_Prenom FROM IDENTITE WHERE Ide_ID IN (select Ens_Num from ENSEIGNANT)");
                if (array == null) return;
                IDList.Clear();
                while (array.Read())
                {
                    cmbUser.Items.Add((int)array.GetDecimal(0) + " - " + array.GetString(1) + " " + array.GetString(2));
                    IDList.Add((int)array.GetDecimal(0));
                }
            }
            TypeAcc = cmbTypeAccount.SelectedIndex;

            //Etudiant
            
            if (cmbTypeAccount.SelectedIndex == 3)
            {
                cmbClasses.Enabled = true;
                cmbCampus.Enabled = true;
                cmbCursus.Enabled = true;
                cmbUser.Enabled = true;
                //CampusList
                OracleDataReader array = OracleConnector.Query("select * from campus");

                if (array == null) return;

                while (array.Read())
                {
                    cmbCampus.Items.Add(array.GetString(1));
                }
                TypeAcc = cmbTypeAccount.SelectedIndex;
            }
        }

        private void ClearCtrl()
        {
            cmbClasses.Items.Clear();
            cmbCampus.Items.Clear();
            cmbUser.Items.Clear();
            cmbCursus.Items.Clear();
            cmbCursus.Text = "";
            cmbClasses.Text = "";
            cmbCampus.Text = "";
            cmbUser.Text = "";
        }

        private void cmbCampus_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbUser.Items.Clear();
            cmbUser.Text = "";
            //Campus manager
            if (cmbTypeAccount.SelectedIndex == 1)
            {
                int numCampus = 0;
                OracleDataReader array20 = OracleConnector.Query("SELECT CAM_NUM FROM CAMPUS WHERE CAM_NOM = '" + cmbCampus.SelectedItem.ToString() + "'");
                if (array20 == null) return;
                while (array20.Read())
                    numCampus = (int)array20.GetDecimal(0);

                OracleDataReader array = OracleConnector.Query("select distinct(Ide_ID), Ide_Nom, Ide_Prenom, Cm_Num from IDENTITE, CAMPUSMANAGER WHERE IDENTITE.Ide_ID = CAMPUSMANAGER.Cm_Num and Cm_NUm IN (select Ger_CmNum from CM_CAMPUS WHERE Ger_CampusNum = " + numCampus + ")");
                if (array == null) return;
                IDList.Clear();
                while (array.Read())
                {
                    cmbUser.Items.Add((int)array.GetDecimal(0) + " - " + array.GetString(1) + " " + array.GetString(2));
                    IDList.Add((int)array.GetDecimal(0));
                }
            }

            //Etudiant
            if (cmbTypeAccount.SelectedIndex == 3)
            {

               OracleDataReader array = OracleConnector.Query("select CC_CursusNum from CAMPUS_CURSUS WHERE CC_CampusNum IN (select Cam_Num from CAMPUS WHERE Cam_Nom = '" + cmbCampus.SelectedItem.ToString() + "')");
               if (array == null) return;
                while (array.Read())
                {
                    cmbCursus.Items.Add(array.GetString(0));
                }
            }

            }

        private void cmbCursus_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Etudiant
            cmbUser.Items.Clear();
            cmbUser.Text = "";

            int numCampus = 0;
            OracleDataReader array20 = OracleConnector.Query("SELECT CAM_NUM FROM CAMPUS WHERE CAM_NOM = '" + cmbCampus.SelectedItem.ToString() + "'");
            if (array20 == null) return;
            while (array20.Read())
            {
                numCampus = (int)array20.GetDecimal(0);
            }
            OracleDataReader array = OracleConnector.Query("select Ide_Id, Ide_Nom, Ide_Prenom, Etu_Num from IDENTITE, ETUDIANT WHERE IDENTITE.Ide_ID = ETUDIANT.Etu_Num and Etu_Num IN (select ECP_EtudiantNum from ETUDIANT_CAMPUS_PERIODE WHERE ECP_CampusNum = " + numCampus + ") and Etu_Num IN (select EC_EtudiantNum from ETUDIANT_CURSUS WHERE EC_CursusNom = '" + cmbCursus.SelectedItem.ToString() + "')") ;
            if (array == null) return;
            IDList.Clear();
            while (array.Read())
            {
                cmbUser.Items.Add((int)array.GetDecimal(0) + " - " + array.GetString(1) + " " + array.GetString(2));
                IDList.Add((int)array.GetDecimal(0));

            }

            
        }

        private void cmbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            ID = (int)IDList[int.Parse(cmbUser.SelectedIndex.ToString())];

            cmbOk.Enabled = true;  
        }

        private void cmdReset_Click(object sender, EventArgs e)
        {
            this.Close();
        }
               
        }

     }


